package com.gexiaobao.pigeon.ui.activity

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.ContextCompat
import com.azhon.appupdate.listener.OnButtonClickListener
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.App
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.Constant
import com.gexiaobao.pigeon.app.ext.RxTextTool
import com.gexiaobao.pigeon.app.util.CacheUtil
import com.gexiaobao.pigeon.app.util.DeviceUtil
import com.gexiaobao.pigeon.app.util.KvUtils
import com.gexiaobao.pigeon.app.util.LanguageUtil
import com.gexiaobao.pigeon.app.util.MoreTextUtil
import com.gexiaobao.pigeon.databinding.ActivityWelcomeBinding
import com.gexiaobao.pigeon.ui.activity.bank.ActivitySigningDocument
import com.gexiaobao.pigeon.ui.dialog.PrivacyDialog
import com.gexiaobao.pigeon.ui.fragment.mine.pigeon.activity.ActivityShedRace
import com.gexiaobao.pigeon.ui.organizer.ActivityMemberInfo
import com.gexiaobao.pigeon.ui.pilot.activity.ReleaseMissionActivity
import com.gexiaobao.pigeon.viewmodel.MainViewModel
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import me.hgj.jetpackmvvm.util.startActivity


/**
 * @Author      : hxw
 * @Date        : 2023/5/31 9:49
 * @Describe    : 欢迎页
 */
class WelcomeActivity : BaseActivity<MainViewModel, ActivityWelcomeBinding>(), OnButtonClickListener {

    private lateinit var language: String
    private var mRole: Int = 0
    private var currentVersion = ""
    private var currentVersionCode = 0
    private var isLogin: Boolean = false

    override fun initView(savedInstanceState: Bundle?) {
        setStatusBarTranslucent()
        language = CacheUtil.getLanguageApp()
        isLogin = CacheUtil.isLogin()
        currentVersion = DeviceUtil.getVerName(this)
        currentVersionCode = DeviceUtil.getVersionCode(this)
        if (CacheUtil.isFirst()) {
            showPrivacyDialog()
        } else {
            animGotoMain()
        }
    }

    private var mDialogBehavior: BottomSheetBehavior<FrameLayout>? = null

    @SuppressLint("MissingInflatedId")
    private fun showPrivacyDialog() {
        val dialogBottom = BottomSheetDialog(this, R.style.BottomSheetDialog)
        val dialogView: View = LayoutInflater.from(this).inflate(R.layout.bottom_privacy, null)
        val tvContent = dialogView.findViewById<AppCompatTextView>(R.id.tvPrivacyContent)
        val tvPrivacyAndAgreement = dialogView.findViewById<AppCompatTextView>(R.id.tvPrivacyAndAgreement)
        val tvAgreeAndContinue = dialogView.findViewById<AppCompatTextView>(R.id.tvAgreeAndContinue)
        val tvDisagree = dialogView.findViewById<AppCompatTextView>(R.id.tvDisagree)

        tvContent.movementMethod = LinkMovementMethod.getInstance()
        tvPrivacyAndAgreement.movementMethod = LinkMovementMethod.getInstance()

        RxTextTool.getBuilder(getString(R.string.You_can_read_the_full)).setTextColor(ContextCompat.getColor(this, R.color.comm_text_gray))
            .append(getString(R.string.User_service_agreement)).setTextColor(ContextCompat.getColor(this, R.color.colorPrimary))
            .setClickSpan(clickSpanService)
            .append(getString(R.string.and))
            .append(getString(R.string.privacy_policy)).setTextColor(ContextCompat.getColor(this, R.color.colorPrimary))
            .setClickSpan(clickSpanPrivacy)
            .append(getString(R.string.To_learn_more)).into(tvPrivacyAndAgreement)

        RxTextTool.getBuilder("")
            .append(
                getString(R.string.Thank_you_for_trusting)
            )
            .append(getString(R.string.User_service_agreement)).setTextColor(Color.parseColor("#4fbbff")).setClickSpan(clickSpanService)
            .append(getString(R.string.and))
            .append(getString(R.string.privacy_policy)).setTextColor(Color.parseColor("#4fbbff")).setClickSpan(clickSpanPrivacy)
            .append(getString(R.string.For_what_you_need_to_focus_on))
            .append(getString(R.string.To_learn_more_click)).setTextColor(Color.BLACK).setBold()
            .append(getString(R.string.look_over))
            .append(getString(R.string.Special_note_to_you))
            .setTextColor(Color.BLACK).setBold()
            .append(getString(R.string.You_will_not_be_able_to_use_our_products_and_services))
            .into(tvContent)

        MoreTextUtil.setMore(tvContent)

        tvAgreeAndContinue.setOnClickListener {
            dialogBottom.dismiss()
            animGotoMain()
            CacheUtil.setFirst(false)
        }

        tvDisagree.setOnClickListener {
            showDisAgreeDialog()
        }
        dialogBottom.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialogBottom.setContentView(dialogView)
        mDialogBehavior = dialogBottom.behavior
        mDialogBehavior!!.state = BottomSheetBehavior.STATE_EXPANDED
        dialogBottom.setCancelable(false)
        dialogBottom.setCanceledOnTouchOutside(false)
        dialogBottom.show()
    }

    private fun showDisAgreeDialog() {
        val dialog = PrivacyDialog(this)
        dialog.setContent(getString(R.string.Please_read_it_carefully))
        dialog.setCanceledOnTouchOutside(false)
        dialog.setFullScreenWidth()
        val clickSpan: ClickableSpan = object : ClickableSpan() {
            override fun onClick(widget: View) {
                startActivity<ActivityPrivacyAndService>("type" to "1")
            }

            override fun updateDrawState(ds: TextPaint) {
                ds.isUnderlineText = false
            }
        }
        val clickSpanService: ClickableSpan = object : ClickableSpan() {
            override fun onClick(widget: View) {
                startActivity<ActivityPrivacyAndService>("type" to "2")
            }

            override fun updateDrawState(ds: TextPaint) {
                ds.isUnderlineText = false
            }
        }
        dialog.tvContent.movementMethod = LinkMovementMethod.getInstance()
        RxTextTool.getBuilder("").append(getString(R.string.If_you_disagree))
            .append(getString(R.string.The_user_uses_the_service_agreement)).setTextColor(ContextCompat.getColor(this, R.color.colorPrimary))
            .setUnderline()
            .setClickSpan(clickSpanService)
            .append(getString(R.string.and))
            .append(getString(R.string.User_privacy_Agreement)).setTextColor(ContextCompat.getColor(this, R.color.colorPrimary)).setUnderline()
            .setClickSpan(clickSpan)
            .append(getString(R.string.We_will_not_be_able_to_app))
            .into(dialog.tvContent)
        dialog.btnAgree.setOnClickListener {
            dialog.dismiss()
            CacheUtil.setFirst(false)
            animGotoMain()
            finish()
        }
        dialog.show()
    }

    private val clickSpanService: ClickableSpan = object : ClickableSpan() {
        override fun onClick(widget: View) {
            startActivity<ActivityPrivacyAndService>("type" to "2")
        }

        override fun updateDrawState(ds: TextPaint) {
            ds.isUnderlineText = false
        }
    }

    private val clickSpanPrivacy: ClickableSpan = object : ClickableSpan() {
        override fun onClick(widget: View) {
            startActivity<ActivityPrivacyAndService>("type" to "1")
        }

        override fun updateDrawState(ds: TextPaint) {
            ds.isUnderlineText = false
        }
    }

    private fun animGotoMain() {
        mDatabind.conLayoutWelcome.postDelayed({
            mRole = KvUtils.decodeInt(Constant.ROLE)!!
            if (isLogin) {
                when (mRole) {//4-用户，3-放飞员，1-组织方 5-银行签约 2-公棚收鸽
                    4 -> {
                        startActivity<MainActivity>()
                        LanguageUtil.changeAppLanguage(this, language, MainActivity::class.java)
                    }

                    3 -> {
                        startActivity<ReleaseMissionActivity>()
                        LanguageUtil.changeAppLanguage(this, language, ReleaseMissionActivity::class.java)
                    }

                    1 -> {
                        LanguageUtil.changeAppLanguage(this, language, ActivityMemberInfo::class.java)
                        startActivity<ActivityMemberInfo>()
                    }

                    5 -> {
                        LanguageUtil.changeAppLanguage(this, language, ActivitySigningDocument::class.java)
                        startActivity<ActivitySigningDocument>()
                    }

                    2 -> {
                        LanguageUtil.changeAppLanguage(this, language, ActivityShedRace::class.java)
                        startActivity<ActivityShedRace>("type" to 1)
                    }

                    else -> {
                        LanguageUtil.changeAppLanguage(this, language, MainActivity::class.java)
                        startActivity<MainActivity>()
                    }
                }
            } else {
                LanguageUtil.changeAppLanguage(this, language, LoginActivity::class.java)
                startActivity<LoginActivity>()
            }
            /** 带点渐变动画 */
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
            finish()
        }, 1000)
    }

    override fun onButtonClick(id: Int) {
        if (id == 1) {
            App.exitAll()
        }
    }

}